/* 
 * Copyright (c) 2004-2005 Massachusetts Institute of Technology. This code was
 * developed as part of the Haystack (http://haystack.lcs.mit.edu/) research 
 * project at MIT. Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including without 
 * limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions: 
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE. 
 */
package edu.mit.csail.relo.parts;

import java.io.IOException;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.MidpointLocator;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.tools.DirectEditManager;
import org.eclipse.jface.viewers.TextCellEditor;
import org.openrdf.rio.RdfDocumentWriter;

import edu.mit.csail.relo.parts.AnnoLabelDirectEditPolicy.AnnoLabelSource;
import edu.mit.csail.relo.store.ReloRdfRepository;
import edu.mit.csail.relo.store.StoreUtil;


/**
 */
public class NamedRelationPart extends ArtifactRelEditPart implements AnnoLabelSource {
    
    @Override
    protected IFigure createFigure() {
	    PolylineConnection conn = (PolylineConnection) super.createFigure();
	    conn.setLineStyle(Graphics.LINE_DASH);

        lbl = new Label("");//<<unnamed>>
        conn.add(lbl, new MidpointLocator(conn, 0));

        return conn;
	}

    /**
     * @param rdfWriter
     * @throws IOException
     */
    @Override
    protected void writeRDF(RdfDocumentWriter rdfWriter) throws IOException {
        super.writeRDF(rdfWriter);
        rdfWriter.writeStatement(
                viewRes, 
                CommentEditPart.commentTxt, 
                StoreUtil.createMemLiteral(getAnnoLabelText()) );
        //rdfWriter.writeStatement(getArtifactRel().relationRes, StoreUtil.createMemURI(RDF.TYPE), ReloCore.namedRel);
    }

    /**
     * @param rdfWriter
     * @throws IOException
     */
    @Override
    protected void readRDF(ReloRdfRepository queryRepo) {
        // need to write basic data before we reposition and initialize figure (in base class)
        String relTxt = queryRepo.getStatement(viewRes, CommentEditPart.commentTxt, null).getObject().toString();
        setAnnoLabelText(relTxt);
        //logger.debug("After setText: " + comment.getText());
        
        super.readRDF(queryRepo);
    }

    
    protected DirectEditManager manager;
    protected void performDirectEdit() {
        if (manager == null)
            manager = new AnnoLabelDirectEditManager(
                            this, 
                            TextCellEditor.class, 
                            new AnnoLabelCellEditorLocator(getAnnoLabelFigure()), 
                            getAnnoLabelFigure());
        manager.show();
    }

    @Override
    public void performRequest(Request request) {
        if (request.getType() == RequestConstants.REQ_DIRECT_EDIT)
            performDirectEdit();
        else
            super.performRequest(request);
    }
    @Override
    protected void createEditPolicies() {
        super.createEditPolicies();
        installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new AnnoLabelDirectEditPolicy(this));
    }


    Label lbl = null;

    public Label getAnnoLabelFigure() {
        return lbl;
    }
    public void setAnnoLabelText(String text) {
        getAnnoLabelFigure().setText(text);
    }
    public String getAnnoLabelText() {
        return getAnnoLabelFigure().getText();
    }
    
}